package com.ruoyi.shop.controller;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.shop.domain.GoodsImg;
import com.ruoyi.shop.domain.GoodsImgList;
import com.ruoyi.shop.service.IGoodsImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/backstage/goodsImg")
public class goodsImgController {
    @Autowired
    private IGoodsImgService goodsImgService;


    @GetMapping("/list/{gid}")
    public AjaxResult getGoodsImgList(@PathVariable("gid") Integer gid) {
        List<GoodsImg> list = goodsImgService.getGoodsImgList(gid);
        for (GoodsImg img : list) {
            img.setUrl("/profile/upload/" + img.getUrl());
        }

        return AjaxResult.success(list);
    }


    @PutMapping("/insertImg")
    public AjaxResult insertGoodsImg(@RequestBody GoodsImgList goodsImg) {
        Integer gid = goodsImg.getGid();
        goodsImgService.delGoodsImg(gid);

        for (int i = 0; i < goodsImg.getImgList().length; i++) {
            GoodsImg img = new GoodsImg();
            img.setGoodsId(gid);
            img.setUrl(StrUtil.removePrefix(goodsImg.getImgList()[i], "/profile/upload/"));
            img.setStatus(0);

            goodsImgService.insertGoodsImg(img);
        }


        return AjaxResult.success();
    }
}
